# -*- coding: utf-8 -*-
import os
import sys
import PySimpleGUI as sg
import wget
from openpyxl import Workbook


def load(date_info):
    out_filename = 'report.txt'
    if os.path.isfile(out_filename):
        # 删除下载文件
        os.remove(out_filename)
    # 网络地址
    data_url = "https://gitee.com/dogsled/lddb_chandao/raw/master/chandao_report/report.md"
    wget.download(data_url, out=out_filename)
    file_content = open(out_filename, mode="r", encoding="utf-8").readlines()
    # for x in file_content:
    #     print(x)
    filter_content = filter(lambda x: not (x.__contains__("日期") or x.__contains__("|-")), file_content)
    cur_data_str = date_info
    # cur_data_str = "2022-03-17"
    filter_current_content = filter(lambda x: x.__contains__(cur_data_str), filter_content)
    arr = []
    for x in filter_current_content:
        arr.append(x.split("|"))
        # print(x.split("|"))
    # print(arr)
    return arr


def arr(arrs, index):
    arr_new = []
    for arr in arrs:
        arr_new.append(arr[index])
    # print(arr_new)
    return arr_new


if __name__ == '__main__':
    current_path = sys.path[0]

    layout = [[sg.Text("输入账期(例:04w3):", size=25), sg.Input('', key='date')],
              [sg.Button('确定')]]

    # 创建窗口
    window = sg.Window('周报生成', layout)  # Part 3 - 窗口定义

    while True:
        # Display and interact with the Window
        event, values = window.read()  # Part 4 - 开启主循环 window.read()
        if event is None:
            exit()
            break
        elif event == '确定':
            date_info = values['date']
            print(date_info)
            break
    window.close()
    arrs = load(date_info)
    arr_new = arr(arrs, 1)
    Work_hours = len(set(arr_new))
    print(Work_hours)
    user_name = set(arr(arrs, 3))

    columns = ['姓名', '账号', '联系方式', '本周工作内容', '正常工时（人天）', '工作日加班工时（人天）', '备注']

    workbook = Workbook()
    sheet = workbook.active
    sheet.title = date_info
    sheet.append(columns)
    for one_user in user_name:
        i = 1
        work_info = []
        name = ""
        account = ""
        phone = ""
        result_final = ""
        for ary in arrs:
            if ary.__contains__(one_user):
                # one_text = str(i) + "." + ary[5] + "\r\n"
                # work_info = work_info + one_text
                work_info.append(ary[5])
                name = ary[7]
                account = ary[3]
                phone = ary[8]
        # print(one_text)
        # print(work_info)
        distinct_infos = set(work_info)
        for distinct_info in distinct_infos:
            one_text = str(i) + "." + distinct_info + "\r\n"
            result_final = result_final + one_text
            i = i + 1
        one_arr = [name, account, phone, result_final, Work_hours, '0', '']
        sheet.append(one_arr)
        print(one_arr)
    print(user_name)
    # result_final.to_csv(file_path, sep='\t', index=False, header=False)
    # print(arrs)
    workbook.save(date_info + '.xlsx')
    # df.to_excel('./data2.xlsx')
    # exit()
